/** @file bitmap_button.cpp
 */

#include "bitmap_button.h"
#include "stdio.h"

#include "gdi.h"

BitmapButton::BitmapButton(HWND parent, Bitmap *bitmap):Widget(parent)
{
    activable =1;
    name ="BitmapButton";
    state =0;
    
    if(bitmap)
    {
        this->bitmap =bitmap;
        rect.x1 =rect.x0 + bitmap->w+4;
        rect.y1 =rect.y0 + bitmap->h+4;
    }else
    {
        rect.x1=rect.x0;rect.y1 =rect.y0;
    }

}

BitmapButton::~BitmapButton()
{
}

void BitmapButton::setupWidgets()
{
}

void BitmapButton::setBitmap( Bitmap *bmp)
{
    this->bitmap =bmp;
    
    if(bitmap)
    {
        rect.x1=rect.x0 + bmp->w+4;
        rect.y1=rect.y0 + bmp->h+4;
    }
}

void BitmapButton::draw(Rect *r)
{
    int t_x,t_y;
    char *pChar;

    pDC->fillRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x00);
    
    pChar =bitmap->data;
    
    if(isActive())
    {
        if(state)
        {
            pDC->drawRect(rect.x0+1,rect.y0+1, rect.x1-1, rect.y1-1, 0x0F);            
        }
        else
        {
            pDC->drawRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x0F);
        }
    }

    //draw bmp
    for(t_y=rect.y0+2; t_y<rect.y1-2; t_y++)
    {
        for(t_x=rect.x0+2; t_x<rect.x1-2; t_x++)
        {
            pDC->drawPoint(t_x,t_y, *pChar++);
        }
    }
    
    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}

int BitmapButton::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
    case MSG_KEY_DOWN:
        switch(wParam)
        {
        case KEY_ENTER:
            if(state ==0)
            {
                state =1;
                sendMessage(this, MSG_PAINT, 0,0);
            
                //emit BUTTON DOWN
                signal[SIGNAL_BMP_BUTTON_DOWN].emitSignal();
            }
            break;
        }
        break;
        
    case MSG_KEY_UP:
        switch(wParam)
        {
        case KEY_ENTER:
            if(state ==1)
            {
                state =0;
                sendMessage(this, MSG_PAINT, 0,0);
            
                //emit BUTTON UP
                signal[SIGNAL_BMP_BUTTON_UP].emitSignal();
            }
            break;
        }
        break;
    }
    
    return Widget::eventHandler(hWnd, iMsg, wParam, lParam);
}

